package cn.lyd.system.controller;

import cn.lyd.basic.util.AjaxResult;
import cn.lyd.basic.util.PageList;
import cn.lyd.system.domain.Dictionarydetail;
import cn.lyd.system.query.DictionarydetailQuery;
import cn.lyd.system.service.IDictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "数据字典明细的API",description="数据字典明细相关的CRUD功能")
@RestController
//@Controller
//@ResponseBody

@RequestMapping("/dictionarydetail")
public class DictionarydetailController {

    @Autowired
    private IDictionarydetailService dictionarydetailService;


    /**
     * 接口：查询单个数据
     * @param id
     * @return
     */
    //前端传参 get http://localhost:8080/department/1

    //后端接收参数 @GetMapping("/department/{id}") + @PathVariable("id") Long id
        //@GetMapping等价于：@RequestMapping(     method = {RequestMethod.GET}   )
        //@PathVariable 从路径中获取变量
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Dictionarydetail findById(@PathVariable("id") Long id){
        return dictionarydetailService.findById(id);
    }

    /**
     * 查询全部
     * @return
     */
    //前端传参  get http://localhost:8080/department {对象}

    //后端接收参数 @GetMapping("/department")
    @GetMapping
    @ApiOperation(value = "查询全部" )
    public List<Dictionarydetail> findAll(){
        return dictionarydetailService.findAll();
    }


    /**
     * 接口：分页查询
     * @param dictionarydetailQuery
     * @return
     */
    //前端传参 post http://localhost:8080/department 对象

    //后端接收参数 @PostMapping("/department")+ @RequestBody Department department
    //@PostMapping等价于：@RequestMapping(     method = {RequestMethod.POST}   )
    //@RequestBody从请求体【请求数据包中】中获取数据

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Dictionarydetail> queryPage(@RequestBody DictionarydetailQuery dictionarydetailQuery){
      return  dictionarydetailService.queryPage(dictionarydetailQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    //delete http://localhost:8080/department/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            //如果删除成功，调用AjaxResult的无参构造
            dictionarydetailService.del(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    /**
     * 添加或修改
     * @param dictionarydetail
     * @return
     */
    //put http://localhost:8080/department {对象}
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){
        try {
            //判断有无id--判断是添加还是修改
            if (dictionarydetail.getId()==null){
                 dictionarydetailService.add(dictionarydetail);
            }else {
                dictionarydetailService.update(dictionarydetail);
            }
            //操作成功调用AjaxResult的无参构造
            return new AjaxResult();
        } catch (Exception e) {//操作失败
            e.printStackTrace();
            //操作失败调用AjaxResult的有参构造
            return new AjaxResult(false,"操作失败");
        }

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除对象" )
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            dictionarydetailService.patchDel(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


}
